
#include "PhaseBiasBase.hpp"

using namespace std;

namespace gpstk
{

   string PhaseBiasBase::writeTime(const CivilTime& dt) const
   {
      if (dt == CommonTime::BEGINNING_OF_TIME)
      {
         return std::string(26, ' ');
      }
      
      std::string line;
      line  = StringUtils::rightJustify(
         StringUtils::asString<short>(dt.year), 4);
      line += StringUtils::rightJustify(
         StringUtils::asString<short>(dt.month), 3);
      line += StringUtils::rightJustify(
         StringUtils::asString<short>(dt.day), 3);
      line += StringUtils::rightJustify(
         StringUtils::asString<short>(dt.hour), 3);
      line += StringUtils::rightJustify(
         StringUtils::asString<short>(dt.minute), 3);
      line += StringUtils::rightJustify(
         StringUtils::asString(dt.second, 6), 10);
      
      return line;
   }  // writeTime

   
   CivilTime PhaseBiasBase::parseTime(const string& line) const
      noexcept(false)
   {
      if ( line.size() != 26 )
      {
         FFStreamError e("Invalid time string length: " + 
                         StringUtils::asString(line.size()));
         GPSTK_THROW(e);
      }
      
      if ( line == std::string(26, ' ') )
      {
         return CommonTime::BEGINNING_OF_TIME;
      }
      
      int year, month, day, hour, min;
      double sec;
      
      year = StringUtils::asInt( line.substr(0, 4) );
      month = StringUtils::asInt( line.substr(4, 3) );
      day = StringUtils::asInt( line.substr(7, 3) );
      hour = StringUtils::asInt( line.substr(10, 3) );
      min = StringUtils::asInt( line.substr(13, 3) );
      sec = StringUtils::asDouble( line.substr(16, 10) );
      
      return CivilTime(year, month, day, hour, min, sec);

   }  // parseTime
   
   
}  // namespace

